#include"funs.h"

//获取电脑所有盘符名
QStringList funs::getDiskName()
{
    QFileInfoList list = QDir::drives();
    QStringList diskNameList(NULL);

    for (int i=0; i<list.count(); i++)
    {

        QString str = list.at(i).absoluteDir().absolutePath();
        diskNameList.append(str);
    }
    return diskNameList;
}


//获取磁盘容量
quint64 funs::getDiskSpace(QString iDriver, bool flag)
{
    //iDriver为盘符(例如"C\"),flag为true则求磁盘总容量，为false则求磁盘剩余容量
    LPCWSTR strDriver=(LPCWSTR)iDriver.utf16();
    ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;

    //调用函数获取磁盘参数(单位为字节Byte),转化为GB，需要除以(1024*1024*1024)
    GetDiskFreeSpaceEx( strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace) ;
    if(flag)
    {
        return (quint64) totalDiskSpace.QuadPart/GB;
    }
    else
    {
        return (quint64) totalFreeDiskSpace.QuadPart/GB;
    }
}

double funs::getCpuUsage()
{

//    static FILETIME s_preIdleTime = { 0, 0 };
//    static FILETIME s_preKernelTime = { 0, 0 };
//    static FILETIME s_preUserTime = { 0, 0 };
//    static bool isFirst = true;

//    FILETIME idleTime;
//    FILETIME kernelTime;
//    FILETIME userTime;

//    int idleCost = 0;
//    int kernelCost = 0;
//    int userCost = 0;

//    double rate = 0.0;

//    GetSystemTimes(&idleTime, &kernelTime, &userTime);

//    idleCost = CompareFileTime(&s_preIdleTime, &idleTime);
//    kernelCost = CompareFileTime(&s_preKernelTime, &kernelTime);
//    userCost = CompareFileTime(&s_preUserTime, &userTime);

//    s_preIdleTime = idleTime;
//    s_preKernelTime = kernelTime;
//    s_preUserTime = userTime;

//    if (kernelCost + userCost == 0) {
//        return -1;
//    } else if (isFirst) {
//        isFirst = false;
//        return 0.0;
//    } else {
//        rate = (double)(kernelCost + userCost - idleCost) * 100 / (kernelCost + userCost);
//        return rate;
//    }
    HANDLE hEvent;
    bool res;
    static FILETIME preIdleTime;
    static FILETIME preKernelTime;
    static FILETIME preUserTime;
    FILETIME idleTime;
    FILETIME kernelTime;
    FILETIME userTime;
    res = GetSystemTimes(&idleTime,&kernelTime,&userTime);
    preIdleTime = idleTime;
    preKernelTime = kernelTime;
    preUserTime = userTime;
    hEvent = CreateEvent(nullptr,FALSE,FALSE,nullptr);//初始值为nonsignaled
    WaitForSingleObject(hEvent,500);//等待500毫秒
    res = GetSystemTimes(&idleTime,&kernelTime,&userTime);
    long long idle = CompareFileTime(preIdleTime,idleTime);
    long long kernel = CompareFileTime(preKernelTime,kernelTime);
    long long user = CompareFileTime(preUserTime,userTime);
    double nCpuRate =ceil( 100.0*( kernel + user - idle ) / ( kernel + user ) );
    //qDebug()<<"windows:CPU use rate:"<<nCpuRate<<"%";
    return nCpuRate;

}

__int64 funs::CompareFileTime(FILETIME preTime,FILETIME nowTime)
{
    return Filetime2Int64(&nowTime) - Filetime2Int64(&preTime);
}

__int64 funs::Filetime2Int64(const FILETIME* ftime)
{
    LARGE_INTEGER li;
    li.LowPart = ftime->dwLowDateTime;
    li.HighPart = ftime->dwHighDateTime;
    return li.QuadPart;
}

bool funs::memory(double &nMemTotal,double &nMemUsed)
{
    MEMORYSTATUSEX memsStat;
    memsStat.dwLength = sizeof(memsStat);
    if(!GlobalMemoryStatusEx(&memsStat))//如果获取系统内存信息不成功，就直接返回
    {
        return false;
    }
    double nMemFree = memsStat.ullAvailPhys/( 1024.0*1024.0 );
    nMemTotal = memsStat.ullTotalPhys/( 1024.0*1024.0 );
    nMemUsed= nMemTotal- nMemFree;
    qDebug("windows:mem total:%.0lfMB,use:%.0lfMB",nMemTotal,nMemUsed);
    return true;
}
